<?php

namespace app\common\library;

use app\common\library\Exception;
use app\common\model\UserAudit;
use app\common\model\TrainActiveInfo;
use app\common\model\ActiveInfo;
use app\common\model\TrainActive;
use app\common\model\Active;
use app\common\model\UserCard;
use app\common\model\User;
use app\common\model\Member;
use app\common\model\StoreConfig;

// 回显数据
class EchoData
{

    /**
     * 注册会员的时候回显的数据
     * 
     * @param int $userID 用户ID
     * @return array
     */
    public function registerMemberPageEchoData($userID)
    {
        if (empty($userID)) {
            new Exception('Params error');
        }

        // 申请过会员：使用之前注册的资料
        $UserAudit = new UserAudit();
        $echoData = $UserAudit->where('uid', $userID)->field('mobile, nickname, industry, sign_photo, idcard_front, idcard_back, idcard_number, idcard_name, sex')->find();
        if (!empty($echoData)) {
            // 身份证反面
            $echoData['idcard_back'] = [
                'url' => $echoData['idcard_back'],
                'show_url' => getRealImg($echoData['idcard_back'])
            ];
            // 签到照片
            $echoData['sign_photo'] = [
                'url' => $echoData['sign_photo'],
                'show_url' => getRealImg($echoData['sign_photo'])
            ];
            // 身份证正面
            $echoData['idcard_front'] = [
                'url' => $echoData['idcard_front'],
                'show_url' => getRealImg($echoData['idcard_front'])
            ];
            return $echoData;
        }

        // 报过名的情况：使用报名资料
        $ActiveInfo = new ActiveInfo();
        $echoData = $ActiveInfo->where('uid', $userID)->order('createtime', 'desc')->field('mobile, name as nickname, industry, user_photo as sign_photo')->find();
        if (!empty($echoData)) {
            // 身份证反面
            $echoData['idcard_back'] = [
                'url' => '',
                'show_url' => ''
            ];
            // 签到照片
            $echoData['sign_photo'] = [
                'url' => $echoData['sign_photo'],
                'show_url' => getRealImg($echoData['sign_photo'])
            ];
            // 身份证正面
            $echoData['idcard_front'] = [
                'url' => '',
                'show_url' => ''
            ];
            // 性别
            $echoData['sex'] = UserAudit::SEX_MALE;
            return $echoData;
        }

        // 有名片资料的情况：使用名片资料
        $UserCard = new UserCard();
        $echoData = $UserCard->where('uid', $userID)->field('mobile, name as nickname, industry, image as sign_photo')->find();
        if (!empty($echoData)) {
            // 身份证反面
            $echoData['idcard_back'] = [
                'url' => '',
                'show_url' => ''
            ];
            // 签到照片
            $echoData['sign_photo'] = [
                'url' => $echoData['sign_photo'],
                'show_url' => getRealImg($echoData['sign_photo'])
            ];
            // 身份证正面
            $echoData['idcard_front'] = [
                'url' => '',
                'show_url' => ''
            ];
            // 性别
            $echoData['sex'] = UserAudit::SEX_MALE;
            return $echoData;
        }

        // 以上信息都没有的情况：回填信息都为空
        $echoData = [
            'mobile' => '', // 手机号码
            'nickname' => '', // 会员名称
            'industry' => '', // 代表行业
            'sign_photo' => [ // 签到照片
                'url' => '', // 提交用的url
                'show_url' => '' // 显示用的url
            ],
            'idcard_back' => [ // 身份证反面
                'url' => '',
                'show_url' => ''
            ],
            'idcard_number' => '', // 身份证号码
            'idcard_name' => '', // 身份证姓名
            'idcard_front' => [ // 身份证正面
                'url' => '',
                'show_url' => ''
            ],
            'sex' => UserAudit::SEX_MALE // 性别
        ];

        return $echoData;
    }


    /**
     * 活动报名页面回显的数据
     * 
     * @param int $userID   用户ID
     * @param int $activeID 活动ID
     * @return array
     */
    public function activeEnrollPageEchoData($userID, $activeID)
    {
        if (empty($userID) || empty($activeID)) {
            new Exception('Params error');
        }



        // 获取用户类型
        $User = new User();

        $userData = $User->where('id', $userID)->field('type, store_id')->find();

        // 获取参加活动费用
        $StoreConfig = new StoreConfig();
        $where = [
            'group' => 'basic',
            'name' => 'active_info_order_fee',
            'store_id' => $userData['store_id']
        ];
        $activeEnrollCost = $StoreConfig->where($where)->value('value');

        // 如果是会员
        if ($userData['type'] == User::TYPE_MEMBER) {
            // 拿会员资料
            $Member = new Member();
            $activeInfoData = $Member->where('uid', $userID)->field('mobile, nickname as name, industry, sign_photo as user_photo')->find();

            $activeInfoData['user_photo'] = [
                'url' => $activeInfoData['user_photo'],
                'show_url' => getRealImg($activeInfoData['user_photo'])
            ];
            $activeInfoData['address'] = '';
            $activeInfoData['user_type'] = ActiveInfo::USER_TYPE_GUEST;
            $activeInfoData['referrer_uid'] = 0;
            $activeInfoData['bin_name'] = '';
            $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

            return $activeInfoData;
        } else { // 非会员
            // 获取最近一次的报名信息
            $ActiveInfo = new ActiveInfo();
            $where = [
                'active_id' => $activeID,
                'uid' => $userID
            ];
            $activeInfoData = $ActiveInfo->where($where)->order('createtime', 'desc')->field('user_type, name, referrer_uid, industry, mobile, bin_name, user_photo, address')->find();
            if (!empty($activeInfoData)) {

                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];
                $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

                return $activeInfoData;
            }

            // 获取会员信息(待审核、审核失败或已过期的会员信息)
            $UserAudit = new UserAudit();
            $activeInfoData = $UserAudit->where('uid', $userID)->field('mobile, nickname as name, industry, sign_photo as user_photo')->find();
            if (!empty($activeInfoData)) {

                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];
                $activeInfoData['address'] = '';
                $activeInfoData['user_type'] = ActiveInfo::USER_TYPE_GUEST;
                $activeInfoData['referrer_uid'] = 0;
                $activeInfoData['bin_name'] = '';
                $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

                return $activeInfoData;
            }

            // 获取名片资料
            $UserCard = new UserCard();
            $activeInfoData = $UserCard->where('uid', $userID)->field('name, industry, mobile, image as user_photo')->find();
            if (!empty($activeInfoData)) {
                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];
                $activeInfoData['address'] = '';
                $activeInfoData['user_type'] = ActiveInfo::USER_TYPE_GUEST;
                $activeInfoData['referrer_uid'] = 0;
                $activeInfoData['bin_name'] = '';
                $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

                return $activeInfoData;
            }

            $activeInfoData = [];
            $activeInfoData['name'] = '';
            $activeInfoData['industry'] = '';
            $activeInfoData['mobile'] = '';
            $activeInfoData['address'] = '';
            $activeInfoData['user_photo'] = [
                'url' => '',
                'show_url' => ''
            ];
            $activeInfoData['user_type'] = ActiveInfo::USER_TYPE_GUEST;
            $activeInfoData['referrer_uid'] = 0;
            $activeInfoData['bin_name'] = '';
            $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

            return $activeInfoData;
        }
    }

    /**
     * 帮嘉宾报名活动页面的数据回显
     * 
     * @param int $guestUserID  嘉宾的用户ID
     * @param int $activeID     活动ID
     * @return array
     */
    public function helpMyGuestEnrollPageEchoData($guestUserID, $activeID)
    {
        if (empty($guestUserID) || empty($activeID)) {
            new Exception('Params error');
        }



        // 获取用户类型
        $User = new User();

        $userData = $User->where('id', $guestUserID)->field('type, store_id')->find();

        // 获取参加活动费用
        $StoreConfig = new StoreConfig();
        $where = [
            'group' => 'basic',
            'name' => 'active_info_order_fee',
            'store_id' => $userData['store_id']
        ];
        $activeEnrollCost = $StoreConfig->where($where)->value('value');


        // 获取最近一次的报名信息
        $ActiveInfo = new ActiveInfo();
        $where = [
            'active_id' => $activeID,
            'uid' => $guestUserID
        ];
        $activeInfoData = $ActiveInfo->where($where)->order('createtime', 'desc')->field('name, industry, mobile, user_photo')->find();
        if (!empty($activeInfoData)) {

            $activeInfoData['user_photo'] = [
                'url' => $activeInfoData['user_photo'],
                'show_url' => getRealImg($activeInfoData['user_photo'])
            ];
            $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

            return $activeInfoData;
        }

        // 获取会员信息(待审核、审核失败或已过期的会员信息)
        $UserAudit = new UserAudit();
        $activeInfoData = $UserAudit->where('uid', $guestUserID)->field('mobile, nickname as name, industry, sign_photo as user_photo')->find();
        if (!empty($activeInfoData)) {

            $activeInfoData['user_photo'] = [
                'url' => $activeInfoData['user_photo'],
                'show_url' => getRealImg($activeInfoData['user_photo'])
            ];
            $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

            return $activeInfoData;
        }

        // 获取名片资料
        $UserCard = new UserCard();
        $activeInfoData = $UserCard->where('uid', $guestUserID)->field('name, industry, mobile, image as user_photo')->find();
        if (!empty($activeInfoData)) {
            $activeInfoData['user_photo'] = [
                'url' => $activeInfoData['user_photo'],
                'show_url' => getRealImg($activeInfoData['user_photo'])
            ];
            $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

            return $activeInfoData;
        }

        $activeInfoData = [];
        $activeInfoData['name'] = '';
        $activeInfoData['industry'] = '';
        $activeInfoData['mobile'] = '';
        $activeInfoData['user_photo'] = [
            'url' => '',
            'show_url' => ''
        ];
        $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

        return $activeInfoData;
    }

    /**
     * 培训活动报名页面回显的数据
     * 
     * @param int $userID   用户ID
     * @param int $activeID 活动ID
     * @return array
     */
    public function trainActiveEnrollPageEchoData($userID, $activeID)
    {
        if (empty($userID) || empty($activeID)) {
            new Exception('Params error');
        }



        // 获取用户类型
        $User = new User();

        $userData = $User->where('id', $userID)->field('type, store_id')->find();

        // 获取参加活动费用
        $TrainActive = new TrainActive();
        if ($userData['type'] == User::TYPE_MEMBER) {
            $activeEnrollCost = $TrainActive->where('id', $activeID)->value('member_enroll_price');
        } else {
            $activeEnrollCost = $TrainActive->where('id', $activeID)->value('user_enroll_price');
        }

        // 如果是会员
        if ($userData['type'] == User::TYPE_MEMBER) {
            // 拿会员资料
            $Member = new Member();
            $activeInfoData = $Member->where('uid', $userID)->field('mobile, nickname as name, industry, sign_photo as user_photo')->find();

            $activeInfoData['user_photo'] = [
                'url' => $activeInfoData['user_photo'],
                'show_url' => getRealImg($activeInfoData['user_photo'])
            ];
            $activeInfoData['referrer_uid'] = 0;
            $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

            return $activeInfoData;
        } else { // 非会员
            // 获取最近一次的报名信息
            $ActiveInfo = new TrainActiveInfo();
            $where = [
                'train_active_id' => $activeID,
                'uid' => $userID
            ];
            $activeInfoData = $ActiveInfo->where($where)->order('createtime', 'desc')->field('name, referrer_uid, industry, mobile, user_photo')->find();
            if (!empty($activeInfoData)) {

                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];
                $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

                return $activeInfoData;
            }

            // 获取会员信息(待审核、审核失败或已过期的会员信息)
            $UserAudit = new UserAudit();
            $activeInfoData = $UserAudit->where('uid', $userID)->field('mobile, nickname as name, industry, sign_photo as user_photo')->find();
            if (!empty($activeInfoData)) {

                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];
                $activeInfoData['referrer_uid'] = 0;
                $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

                return $activeInfoData;
            }

            // 获取名片资料
            $UserCard = new UserCard();
            $activeInfoData = $UserCard->where('uid', $userID)->field('name, industry, mobile, image as user_photo')->find();
            if (!empty($activeInfoData)) {
                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];
                $activeInfoData['referrer_uid'] = 0;
                $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

                return $activeInfoData;
            }

            $activeInfoData = [];
            $activeInfoData['name'] = '';
            $activeInfoData['industry'] = '';
            $activeInfoData['mobile'] = '';
            $activeInfoData['user_photo'] = [
                'url' => '',
                'show_url' => ''
            ];
            $activeInfoData['referrer_uid'] = 0;
            $activeInfoData['active_enroll_cost'] = $activeEnrollCost;

            return $activeInfoData;
        }
    }

    /**
     * 活动报名替代页面回显的数据
     * 
     * @param int $userID   用户ID
     * @param int $activeID 活动ID
     * @return array
     */
    public function activeEnrollReplacePageEchoData($userID, $activeID)
    {
        if (empty($userID) || empty($activeID)) {
            new Exception('Params error');
        }

        // 获取用户类型
        $User = new User();
        $userType = $User->where('id', $userID)->value('type');

        // 如果是会员
        if ($userType == User::TYPE_MEMBER) {
            // 获取上一次安排的替代信息
            $ActiveInfo = new ActiveInfo();
            $where = [
                'uid' => $userID,
                'replace_mobile' => ["<>", ""]
            ];
            $activeInfoData = $ActiveInfo->where($where)->field('replace_mobile, replace_name, user_photo')->order('createtime', 'desc')->find();


            if (!empty($activeInfoData)) {
                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];
                return $activeInfoData;
            }
        } else { // 非会员
            // 获取最近一次的报名信息
            $ActiveInfo = new ActiveInfo();
            $where = [
                'active_id' => $activeID,
                'uid' => $userID
            ];
            $activeInfoData = $ActiveInfo->where($where)->order('createtime', 'desc')->field('name as replace_name, mobile as replace_mobile, user_photo')->find();
            if (!empty($activeInfoData)) {

                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];

                return $activeInfoData;
            }

            // 获取会员信息(待审核、审核失败或已过期的会员信息)
            $UserAudit = new UserAudit();
            $activeInfoData = $UserAudit->where('uid', $userID)->field('mobile as replace_mobile, nickname as replace_name, sign_photo as user_photo')->find();
            if (!empty($activeInfoData)) {

                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];

                return $activeInfoData;
            }

            // 获取名片资料
            $UserCard = new UserCard();
            $activeInfoData = $UserCard->where('uid', $userID)->field('name as replace_name, mobile as replace_mobile, image as user_photo')->find();
            if (!empty($activeInfoData)) {
                $activeInfoData['user_photo'] = [
                    'url' => $activeInfoData['user_photo'],
                    'show_url' => getRealImg($activeInfoData['user_photo'])
                ];



                return $activeInfoData;
            }
        }
        $activeInfoData = [];
        $activeInfoData['replace_name'] = '';
        $activeInfoData['replace_mobile'] = '';
        $activeInfoData['user_photo'] = [
            'url' => '',
            'show_url' => ''
        ];

        return $activeInfoData;
    }
}
